package com.ssh.admin.action;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.common.log.LogFactory;
import com.ssh.common.log.LogName;
import com.ssh.common.util.JsonResult;
import com.ssh.common.util.Page;
import com.ssh.entity.Company;
import com.ssh.manager.CompanyManager;

@Controller
@ParentPackage("admin")
@Scope("prototype")
@Results(value={
@Result(name="success", location="/admin/company/companyInfo.jsp"),
@Result(name="findCompanyByP", type="json", params={"root","page","excludeproperties","page\\.curPageNum,page\\.totalPageNum,page\\.totalCount," +
		"page\\.numberPerPage,page\\.pageData\\[\\d+\\]\\.id,page\\.pageData\\[\\d+\\]\\.name,page\\.pageData\\[\\d+\\]\\.content,page\\.pageData\\[\\d+\\]\\.createDate"}),
@Result(name="deleteCompany", type="json", params={"root","page","excludeproperties","page\\.curPageNum"}),
@Result(name="updateCompany", type="json", params={"root","jr","excludeproperties","jr\\.code"}),
@Result(name="goSuperMarket", location="/admin/superMarket/superMarketList.jsp"),
@Result(name="getCompanyAll", type="json", params={"root","companies","excludeproperties","companies\\[\\d+\\]\\.id,companies\\[\\d+\\]\\.name"})
})
public class CompanyAction extends ActionSupport{
	@Resource(name="companyManagerImpl")
	private CompanyManager companyManager;
	private JsonResult jr=new JsonResult();
	private Company company;
	private List<Company> companies;
	private Page page = new Page();
	private String isDesc;
	private String ids;

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public JsonResult getJr() {
		return jr;
	}

	public void setJr(JsonResult jr) {
		this.jr = jr;
	}

	public String getIsDesc() {
		return isDesc;
	}

	public void setIsDesc(String isDesc) {
		this.isDesc = isDesc;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List<Company> getCompanies() {
		return companies;
	}

	public void setCompanies(List<Company> companies) {
		this.companies = companies;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}
	
	/**
	 * 添加系统
	 * @return
	 * @throws Exception
	 */
	public String addCompany() throws Exception{
		company.setDel(0);
		company.setCreateDate(new Timestamp(System.currentTimeMillis()));
		try {
			company = companyManager.addCompany(company);
		} catch (Exception e) {
			e.printStackTrace();
			LogFactory.getDefaultLog().error("系统添加失败", LogName.API);
		}
		return "success";
	}
	
	/**
	 * 修改
	 * @return
	 */
	public String updateCompany(){
		try {
			company = companyManager.updateCompany(company);
			jr.setCode("y");
		} catch (Exception e) {
			jr.setCode("n");
			e.printStackTrace();
			LogFactory.getDefaultLog().error("系统修改失败", LogName.API);
		}
		return "updateCompany";
	}
	
	/**
	 * 分页查询系统
	 * @return
	 * @throws Exception
	 */
	public String findCompanyByP() throws Exception{
		page.setNumberPerPage(20);
		companyManager.findCompaniesByPage(page, isDesc);
		return "findCompanyByP";
	}
	
	/**
	 * 删除系统实体,返回当前第几页
	 * @return
	 * @throws Exception
	 */
	public String deleteCompany() throws Exception{
		try {
			if(ids!=null && !ids.equals("")){
				String[] d = ids.split(",");
				for (int i = 0; i < d.length; i++) {
					companyManager.deleteCompany(new Long(d[i]));
				}
			}else {
				companyManager.deleteCompany(company.getId());
			}
			int _p = (int) page.getCurPageNum();
			page.setNumberPerPage(20);
			page.setCurPageNum(1);
			companyManager.findCompaniesByPage(page, isDesc);
			if(_p<=page.getTotalPageNum()){
				page.setCurPageNum(_p);
			}else{
				page.setCurPageNum(_p-1);
			}
			jr.setCode("y");
		} catch (Exception e) {
			jr.setCode("n");
			e.printStackTrace();
		}
		return "deleteCompany";
	}
	
	/**
	 * 跳转到门店页面
	 * @return
	 * @throws Exception
	 */
	public String goSuperMarket() throws Exception{
		return "goSuperMarket";
	}
	
	/**
	 * 取出所有公司信息
	 * @return
	 */
	public String getCompanyAll(){
		companies = companyManager.getCompaniesAll();
		return "getCompanyAll";
	}
}
